package com.me;

import com.me.Utils.db.C3P0DBUtil;
import com.me.Utils.db.IDbUtil;
import com.me.dao.DbContext;
import com.me.dao.db.DbSet;
import com.me.model.Goods;
import com.me.model.Order;
import com.me.model.Recommend;
import com.me.model.Type;
import com.me.service.*;

import javax.servlet.http.HttpSessionEvent;
import java.util.Collection;

public class StartUp{

	private void configureServices() {
		Services.addScope(DbContext.class,DbContext.class);
		Services.addScope(UserService.class, UserService.class);
		Services.addScope(GoodsService.class,GoodsService.class);
		Services.addScope(TypeService.class,TypeService.class);
		Services.addScope(OrderService.class,OrderService.class);
		Services.addScope(IDbUtil.class, C3P0DBUtil.class);
	}

	private void perSession(HttpSessionEvent se) {
		DbContext ctx = (DbContext)Services.resolve(DbContext.class);
		DbSet<Type> types = ctx.getTypes();
		DbSet<Goods> goods = ctx.getGoods();
		DbSet<Recommend> recs = ctx.getRecommends();

		Recommend r = recs.find("type","1");
		Goods g = goods.find("id",String.format("%d",r.getGoodsId()));
		se.getSession().setAttribute("types",types.getCollection());
		se.getSession().setAttribute("banner",g);

		Collection<Goods> hotlist = goods.contact(recs).addConstrain("goods.id","recommend.goodsid").
				findAll("recommend.type","2",0,6);
		se.getSession().setAttribute("hotList",hotlist);

		Collection<Goods> newlist = goods.contact(recs).addConstrain("goods.id","recommend.goodsid").
				findAll("recommend.type","3",0,8);
		se.getSession().setAttribute("newList",newlist);
	}
}
